package controllers

import (
	"fly/global"
	"fly/helper"
	"fly/models"
	"fly/req"
	"fly/resp"
	"fly/schema"
	"fly/service"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"github.com/duke-git/lancet/v2/condition"
	"github.com/duke-git/lancet/v2/convertor"
	"net/url"
)

// AirportController 机场
type AirportController struct {
	BaseController
	AirportService service.AirportService
	SysUserService service.SysUserService
	SysDictService service.SysDictService
}

// Save ...
// @Title 添加或者编辑机场
// @Description 添加或者编辑机场
// @Param token header  string	false "授权Token"
// @Param body body req.AirportSaveReq true "添加或者编辑机场"
// @Success 200 {object} resp.AirportResp
// @router /save [post]
func (c *AirportController) Save() {
	con := req.AirportSaveReq{}
	c.CheckReqErrorAbort(c.BindJSON(&con))
	c.ValidReqParams(&con)
	mAirport := models.Airport{}
	c.CheckRespErrorAbort(helper.CopyDeep(&mAirport, con))
	var err error
	id := con.Id
	uid := c.GetReqUserId()
	if con.Id > 0 {
		old, _ := models.GetAirportById(id)
		c.CheckReqErrorAbort(helper.RequireTrue(old != nil, "未找到资源"))
		_ = helper.CopyOut(old, con)
		err = models.UpdateAirportById(old, uid)
	} else {
		id, err = models.AddAirport(&mAirport, uid)
	}
	c.CheckRespErrorAbort(err)
	_ = global.DelCache(fmt.Sprintf(service.AirportCacheSimple, id))
	r := resp.AirportResp{}
	airport, err := models.GetAirportById(id)
	err = resp.Copy2TimeResp(&r, airport)
	c.CheckRespErrorAbort(err)
	r.CreatorName, err = c.SysUserService.GetUserRealNameById(uid)
	c.CheckRespErrorAbort(err)
	c.Success(r)
}

// Del ...
// @Title 删除机场
// @Description 删除机场
// @Param token header  string	false "授权Token"
// @Param path id int true "机场id"
// @Success 200 {int} "id"
// @router /:id [delete]
func (c *AirportController) Del() {
	id, err := convertor.ToInt(c.Ctx.Input.Param(":id"))
	c.CheckReqErrorAbort(err)
	c.CheckReqErrorAbort(helper.RequireTrue(c.SysUserService.IsSuperAdminUser(c.GetReqUserId()), "超级管理员才能操作"))
	taskCount, _ := orm.NewOrm().QueryTable(new(models.FlightTask)).Filter("target_airport_id", id).Count()
	c.CheckReqErrorAbort(helper.RequireTrue(taskCount == 0, "该机场还有任务关联使用"))
	c.CheckRespErrorAbort(models.DeleteAirport(id))
	_ = global.DelCache(fmt.Sprintf(service.AirportCacheSimple, id))
	c.Success(id)
}

// Enable 停用启用
// @Title Enable
// @Description 停用启用
// @Param token header  string	false "授权Token"
// @Param path id int true "字典id"
// @Success 200 {int} "id"
// @router /enable/:id [put]
func (c *AirportController) Enable() {
	id, err := convertor.ToInt(c.Ctx.Input.Param(":id"))
	c.CheckReqErrorAbort(err)
	m, err := models.GetAirportById(id)
	c.CheckAssertAbort(m, "资源不存在")
	m.Enabled = condition.TernaryOperator(m.Enabled == schema.Disable, schema.Enabled, schema.Disable)
	c.CheckRespErrorAbort(models.UpdateAirportById(m, c.GetReqUserId()))
	_ = global.DelCache(fmt.Sprintf(service.AirportCacheSimple, id))
	c.Ok()
}

// Index ...
// @Title 机场列表
// @Description 机场列表
// @Param token header  string	false "授权Token"
// @Param keywords query  string false "搜索字段name"
// @Param pageNum query  int false "当前页"
// @Param pageSize query  int false "每页数量"
// @Param total query  int false "数据总数"
// @Param dicKey query  string false "查询key"
// @Param province query  string false "省份"
// @Param startTime query  int false "录入开始时间"
// @Param endTime query  int false "录入结束时间"
// @Param typeDictId query  string false "机场类型"
// @Success 200 {object} []service.Pagination
// @router / [get]
func (c *AirportController) Index() {
	params, _ := url.ParseQuery(c.Ctx.Request.URL.RawQuery)
	data, pagination := c.AirportService.GetPaginateData(params)
	var airportList []resp.AirportResp
	c.CheckRespErrorAbort(resp.Copy2TimeResp(&airportList, &data))
	for i, airport := range data {
		airportList[i].CreatorName, _ = c.SysUserService.GetUserRealNameById(airport.Creator)
		airportList[i].UpdaterName, _ = c.SysUserService.GetUserRealNameById(airport.Updater)
		airportList[i].AirportName, _ = c.SysDictService.GetDictValueById(airport.TypeDictId)
	}
	pagination.List = airportList
	c.Success(pagination)
}

// List ...
// @Title 所有机场列表
// @Description 所有机场列表-用于首页显示
// @Param token header  string	false "授权Token"
// @Success 200 {object} []resp.AirportResp
// @router /list [get]
func (c *AirportController) List() {
	list, err := c.AirportService.GetAllAirport()
	c.CheckRespErrorAbort(err)
	var airportList []resp.AirportResp
	c.CheckRespErrorAbort(resp.Copy2TimeResp(&airportList, &list))
	for i, airport := range list {
		airportList[i].CreatorName, _ = c.SysUserService.GetUserRealNameById(airport.Creator)
		airportList[i].UpdaterName, _ = c.SysUserService.GetUserRealNameById(airport.Updater)
		airportList[i].AirportName, _ = c.SysDictService.GetDictValueById(airport.TypeDictId)
	}
	c.Success(list)
}

// CountByProvince ...
// @Title 统计各省份机场数
// @Description 统计各省份机场数
// @Param token header  string	false "授权Token"
// @Param type query  int false "机场类型"
// @Success 200 {object} []resp.ProvinceCount
// @router /province/count [get]
func (c *AirportController) CountByProvince() {
	list, err := c.AirportService.GetAirportCounts()
	c.CheckRespErrorAbort(err)
	c.Success(list)
}
